﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tomahawk.Runtime.Logic.UI.Components;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;

namespace Tomahawk.Runtime.Logic.UI
{
    public class XUILabel : XUIPlaceableControl
    {
        #region "Serializable properties"

        [XmlElement("Image")]
        public ImageComponent ImageComponent;

        [XmlElement("Text")]
        public TextComponent TextComponent;

        #endregion

        
        /// <summary>
        /// Ctor.
        /// </summary>
        public XUILabel()
        {
            this.TextComponent = new TextComponent();
            this.ImageComponent.ResourcePool = Tomahawk.Runtime.Resources.ResourceManager.ResourcePool.GameState;
        }

        /// <summary>
        /// Control Initialization
        /// </summary>
        protected override void OnInitialize()
        {
            base.OnInitialize();

            // image components
            this.ImageComponent.Initialize(this.ParentUIScene.CanvasLayer);

            // text component
            this.TextComponent.Initialize(this.ParentUIScene.CanvasLayer);

            //copy position and size to sprites
            this.passPositionToSprites();

            //not acepting focus
            this.CanHaveFocus = false;
        }

        protected override void OnPreBeginPlay()
        {
            base.OnPreBeginPlay();

            // Size store.
            this.TextComponent.TextSize = new Vector2(Position.Width, Position.Height);

            // Labels DO word wrap.
            this.TextComponent.bDoesWordWrap = true;

            // Text setup.
            TextComponent.SetUpText();
        }

        /// <summary>
        /// Controls visibility changes
        /// </summary>
        protected override void OnChangeVisibility(bool newValue)
        {
            base.OnChangeVisibility(newValue);
            this.ImageComponent.SetVisibility(newValue);
            this.TextComponent.SetVisibility(newValue);
        }

        #region "Rendering"
        /// <summary>
        /// Prepares the control for rendering
        /// </summary>
        protected override void OnPrepareSpritesForRendering()
        {
            base.OnPrepareSpritesForRendering();
            this.passPositionToSprites();
        }
        #endregion

        #region "Positioning"

        /// <summary>
        /// Handle a position change
        /// </summary>
        protected override void OnPositionChange()
        {
            base.OnPositionChange();
        }

        /// <summary>
        /// Copy the control position to the sprites
        /// </summary>
        private void passPositionToSprites()
        {
            this.ImageComponent.SetPosition(ref this.renderRect);
            this.ImageComponent.SetRotation(this.renderRot, ref this.renderOrigin);
            this.ImageComponent.SetDepth(this.Position.Depth + 0.1f);
            this.TextComponent.SetPosition(ref this.renderRect);
            this.TextComponent.SetRotation(this.renderRot, ref this.renderOrigin);
            this.TextComponent.SetFontScale(0.5f * (this.renderScale.X + this.renderScale.Y));
            this.TextComponent.SetDepth(this.Position.Depth);
        }

        #endregion

        /// <summary>
        /// Destruction handling
        /// </summary>
        public override void OnBeforeDestroy()
        {
            base.OnBeforeDestroy();
            this.ImageComponent.Destroy();
            this.TextComponent.Destroy();
        }

        #region "Readyness"

        /// <summary>
        /// Returns true when all the resource dependant objects are ready
        /// </summary>
        protected override bool checkResourcesReady()
        {
            return base.checkResourcesReady() &&
                this.ImageComponent.IsReady() &&
                this.TextComponent.IsReady();
        }

        #endregion

        /// <summary>
        /// Get or set the alpha of both image and text components
        /// </summary>
        protected override void OnAlphaChange(byte newAlpha)
        {
            base.OnAlphaChange(newAlpha);
            this.TextComponent.Alpha = newAlpha;
            this.ImageComponent.Alpha = newAlpha;
        }

    }

}
